package com.jeecms.wicool.manager.product.impl;

import java.util.ArrayList;
import java.util.List;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.jeecms.common.hibernate3.Updater;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.product.ProductSkuDao;
import com.jeecms.wicool.entity.product.Product;
import com.jeecms.wicool.entity.product.ProductSku;
import com.jeecms.wicool.manager.product.ProductMng;
import com.jeecms.wicool.manager.product.ProductSkuMng;

@Service
@Transactional
public class ProductSkuMngImpl implements ProductSkuMng {
	
	private ProductSkuDao dao;
	@Autowired
	public void setDao(ProductSkuDao dao) {
		this.dao = dao;
	}
	
	@Transactional(readOnly = true)
	public Pagination getPage(Integer productId,int pageNo, int pageSize) {
		Pagination page = dao.getPage(productId,pageNo, pageSize);
		return page;
	}
	@Override
	public ProductSku findById(Integer id) {
		ProductSku entity = dao.findById(id);
		return entity;
	}

	@Override
	public ProductSku update(ProductSku bean,Integer productId) {
		Product p =productMng.findById(productId);
		bean.setProduct(p);
		Updater<ProductSku> updater = new Updater<ProductSku>(bean);
		bean = dao.updateByUpdater(updater);
		return bean;
	}
	@Override
	public List<ProductSku> updateList(List<ProductSku> listNow, Integer productId) {
		// TODO Auto-generated method stub
		//*****先删除******
		//1.获取该产品下的所有sku实体
		List<ProductSku> listAll= getListProductSku(productId); // 原来的所有sku
		List<Integer> listAllId= new ArrayList<Integer>(); //有所有的sku的skuCode
		List<Integer> listNowId = new ArrayList<Integer>();//当前sku的skuCode
		for(ProductSku all:listAll){
			listAllId.add(all.getId());
		}
		for(ProductSku now:listNow){
			if(now.getId()==null){
				listNowId.add(0);
				continue;
			}
			listNowId.add(now.getId());
		}
		List<Integer> listDelId= new ArrayList<Integer>();
		//2.循环所有sku集合与当前的list进行比较，不存在的说明需要被删除
		for(Integer all:listAllId){
				if(!listNowId.contains(all)){
					listDelId.add(all);
			}
		}
		for(Integer i : listDelId){
			listAll.remove(this.findById(i));
			//删除被remove的实例
			this.deleteById(i);
		}
		
		//******再更新或者添加******
		Updater<ProductSku> updater;
		List<ProductSku> listAdd= new ArrayList<ProductSku>();
			for(ProductSku now:listNow){
				if(now.getId()!=null){
					//更新
					updater = new Updater<ProductSku>(now);
					dao.updateByUpdater(updater);
				}else{
					//新增
					listAdd.add(now);
				}
			}
			
		for(ProductSku p:listAdd){
			save(p, productId);
		}
		listAll.addAll(listAdd);
		return listAll;
	}
	@Override
	public ProductSku save(ProductSku bean,Integer productId) {
		Product p =productMng.findById(productId);
		bean.setProduct(p);
		dao.save(bean);
		return bean;
	}

	@Override
	public ProductSku deleteById(Integer id) {
		ProductSku bean = dao.deleteById(id);
		return bean;
	}
	@Override
	public ProductSku[] deleteByIds(Integer[] ids) {
		ProductSku[] beans = new ProductSku[ids.length];
		for (int i = 0,len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}
	@Autowired
	private ProductMng productMng;
	@Override
	public List<ProductSku> getListProductSku(Integer productId) {
        List<ProductSku> result = dao.getListProductSku(productId);
        if (result == null) {
            result = Lists.newLinkedList();
        }
        return result;
    }

}